package honeypot.wljkzxt.action;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.jfree.chart.ChartUtilities;
import cm.util.hibernate.HibernateSessionFactory;
import com.opensymphony.xwork2.ActionSupport;
import java.awt.Color;
import java.awt.Font;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

public class NetStatAction extends ActionSupport {

	
	    private Session session;
	    private Transaction trans;
	    private String hql;
	    private Query query;
	    private List l;
	    private Date begindate;
	    private Date enddate;
	    
	    public Session getSession() {
			return session;
		}

		public void setSession(Session session) {
			this.session = session;
		}

		public Transaction getTrans() {
			return trans;
		}

		public void setTrans(Transaction trans) {
			this.trans = trans;
		}

		public String getHql() {
			return hql;
		}

		public void setHql(String hql) {
			this.hql = hql;
		}

		public Query getQuery() {
			return query;
		}

		public void setQuery(Query query) {
			this.query = query;
		}

		public List getL() {
			return l;
		}

		public void setL(List l) {
			this.l = l;
		}
		

		public Date getBegindate() {
			return begindate;
		}

		public void setBegindate(Date begindate) {
			this.begindate = begindate;
		}

		public Date getEnddate() {
			return enddate;
		}

		public void setEnddate(Date enddate) {
			this.enddate = enddate;
		}

		public String execute() throws Exception
	{
		  session=HibernateSessionFactory.getSession();
		  trans=session.beginTransaction();
		  hql="select time,count(*) from ScanInfo as scan where scan.status<>:t group by year(scan.time),month(scan.time),day(scan.time)";
//		  hql="select fileTime,count(*) from Net as n group by n.fileTime";
		  query=session.createQuery(hql);
		  query.setString("t","OK");//设置查询参数
//		  System.out.println(query.list());
		  
		  l=query.list();
		  DefaultCategoryDataset dataset=new DefaultCategoryDataset();
		  for(Iterator sit=l.iterator();sit.hasNext();)//迭代取值
	     {
			Object[] objects=(Object[])sit.next();
			Date date=(Date)objects[0];
			//介于起始时间和终止时间
          if(((begindate.before(date))&&(enddate.after(date)))||(begindate.equals(date))||(enddate.equals(date)))
          {
			SimpleDateFormat format = new SimpleDateFormat("dd日"); 
			String s = format.format(date);
			
			long count=(Long)objects[1];
//			System.out.println("日期"+s);
//		    System.out.println(d+"的病毒数量为"+count);
		    dataset.addValue(count, "病毒", s);//设置条形图的值
          }
          }
		  //下面是生成条形图的代码
		  SimpleDateFormat yue = new SimpleDateFormat( 
		  "yyyy年MM月"); 
		  String y = yue.format(new Date()); 
	        JFreeChart chart=ChartFactory.createBarChart3D(y,
					"日期","病毒数量",dataset,PlotOrientation.VERTICAL,
					true,true,false);
		    chart.getTitle().setFont(new Font("宋体",Font.BOLD,15));
			CategoryPlot plot = chart.getCategoryPlot(); 
			NumberAxis vn = (NumberAxis) plot.getRangeAxis(); 
			vn.setAutoRangeIncludesZero(true); 

			CategoryAxis domainAxis = plot.getDomainAxis(); 

			domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
			// Lable 
			Font labelFont = new Font("宋体", Font.BOLD, 12); 

			domainAxis.setLabelFont(labelFont);
			domainAxis.setTickLabelFont(labelFont);

			domainAxis.setMaximumCategoryLabelWidthRatio(1f);
			// domainAxis.setVerticalCategoryLabels(false); 
			plot.setDomainAxis(domainAxis); 

			ValueAxis rangeAxis = plot.getRangeAxis(); 
			rangeAxis.setLabelFont(labelFont);
			rangeAxis.setTickLabelFont(labelFont);
			rangeAxis.setUpperMargin(0.15); 
			rangeAxis.setLowerMargin(0.15); 
			plot.setRangeAxis(rangeAxis); 
			BarRenderer renderer = new BarRenderer(); 
			renderer.setMaximumBarWidth(0.7); 
			renderer.setMinimumBarLength(10); 

			renderer.setBaseOutlinePaint(Color.BLACK); 

			renderer.setSeriesPaint(0, Color.GREEN); 
			renderer.setSeriesPaint(1, new Color(0, 0, 255)); 
			renderer.setItemMargin(0.25); 
			renderer 
			.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); 
			renderer.setBaseItemLabelsVisible(true); 

			plot.setRenderer(renderer); 
			plot.setForegroundAlpha(0.6f); 
			plot.setBackgroundAlpha(0.7f);
			
			chart.getLegend().setItemFont(labelFont);
	        
			HttpServletResponse response = ServletActionContext.getResponse();
			try {
				ChartUtilities.writeChartAsJPEG(response.getOutputStream(), chart,
						700, 300);
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		  
		  session.close();
		  System.out.println("成功");
		
		
		return null;
	}
}
